package com.hnkywl.sts.service.wlgl;

import com.hnkywl.sts.dao.wlgl.XspsDao;
import com.hnkywl.sts.dao.xsgl.XsdDao;
import com.hnkywl.sts.entity.xsgl.Psd;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午10:55
 * To change this template use File | Settings | File Templates.
 */
@Service
public class XspsService {
    private XspsDao xspsDao;
    private BmService bmService;
    private XsdDao xsdDao;

    @Transactional
    public String findAllxsps(String xsdh,String psdh,int pageNo, int pageSize) {
        Page<Psd> xspsPage = this.xspsDao.findXspsList(xsdh,psdh,pageNo, pageSize);
        List<Psd> xspsList = xspsPage.getResult() == null ? new ArrayList<Psd>() : xspsPage.getResult();
        String xspsJson = BaseUtils.fetchCollectionToJson(xspsList, "id","xsd.id","xsd.xsdh","xsd.jhdz","xsd.kh.id","xsd.kh.qc","wls.id","wls.qc","psdh","shch","jsy","lxdh");
        return "{\"total\":" + xspsPage.getTotalCount() + ",\"rows\":[" + xspsJson + "]}";
    }

    public String getPsdbm() {
        return bmService.getPsdbm();
    }

    /**
     * 保存销售配送
     * @param xsps
     */
    @Transactional
    public void saveXsps(Psd xsps) throws SQLException {
        nullId(xsps);
        xsps.setPsdh(this.bmService.getPsdbm());
        xsps.setDateCreated(new Date());
        xsps.setCreateUser(YgService.getCurrentYg());
        xsps.setCreateOrgan(YgService.getCurrentYg().getZggs());
        xsps.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        xsps.setLastUpdated(xsps.getDateCreated());
        Xsd xsd = xsdDao.get(xsps.getXsd().getId());
        xsd.setPsd(xsps);
        xsd.setYps(true);
        this.xspsDao.save(xsps);
        this.xsdDao.save(xsd);
    }

    /**
     * 修改销售配送
     * @param xsps
     */
    @Transactional
    public void updateXsps(Psd xsps) {
        Psd clone = this.findXspsDetail(xsps.getId());
        nullId(xsps);
        clone = (Psd) ReflectionUtils.getInstance(clone, xsps);
        clone.setLastUpdated(new Date());
        this.xspsDao.save(clone);
    }

    /**
     * 删除销售配送
     * @param id
     */
    @Transactional
    public void deleteXsps(Long id) {
        Psd xsps = this.findXspsDetail(id);
        xsps.setLastUpdated(new Date());
        xsps.setDeleted(1);
        xsps.getXsd().setPsd(null);
        xsps.getXsd().setYps(false);
        this.xspsDao.save(xsps);
    }

    /**
     * 查询收款审核明细
     * @param id
     * @return
     */
    @Transactional
    public Psd findXspsDetail(Long id) {
        return this.xspsDao.get(id);
    }

    /**
     *    审核状态
     * @param id
     * @param status
     */
//    @Transactional
//    public void changeStatus(Long id,Integer status) {
//        Jsd xsps = this.findxspsDetail(id);
////        Yg kpr = YgService.getCurrentYg();
////        xsps.setKpr(kpr);
////        xsps.setKprq(new Date());
//        xsps.setZt(status);
//        this.xspsDao.save(xsps);
//    }

    /**
     * 关联判断
     * @param xsps
     */
    private void nullId(Psd xsps){
//        if (xsps != null && xsps.getGlqy() != null && xsps.getGlqy().getQyfr() != null && xsps.getGlqy().getQyfr().getId()==null)
//            xsps.getGlqy().setQyfr(null);
//        if(xsps !=null&&xsps.getSqr()!=null && xsps.getSqr().getId()==null)
//            xsps.setSqr(null);
//        if(xsps !=null && xsps.getGlqy()!=null&& xsps.getGlqy().getId()==null)
//            xsps.setGlqy(null);
//        if(xsps !=null && xsps.getGs()!=null && xsps.getGs().getId()==null)
//            xsps.setGs(null);
    }

  

    //========================get and set==========================
    @Autowired
    public void setXspsDao(XspsDao xspsDao) {
        this.xspsDao = xspsDao;
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setXsdDao(XsdDao xsdDao) {
        this.xsdDao = xsdDao;
    }
}
